#include "AlphaScene.h"
#include "KBEngineApp.h"

bool AlphaScene::init()
{
	CCScene::init();

	CCMenuItemFont* pTitle = CCMenuItemFont::create("Loginapp_reqClose", this, menu_selector(AlphaScene::onLoginappReqClose));
	pTitle->setFontSize(30);
	pTitle->setPosition(ccp(500, 600));

	CCMenuItemFont* pConnect = CCMenuItemFont::create("Connect", this, menu_selector(AlphaScene::onConnectClick));
	pConnect->setFontSize(30);
	pConnect->setPosition(ccp(200, 450));

	CCMenuItemFont* pDisconnect = CCMenuItemFont::create("Disconnect", this, menu_selector(AlphaScene::onDisconnectClick));
	pDisconnect->setFontSize(30);
	pDisconnect->setPosition(ccp(450, 450));

	//Action
		//1:
		CCMenuItemFont* loginAppHello = CCMenuItemFont::create("Loginapp_Hello", this, menu_selector(AlphaScene::Loginapp_hello));
		loginAppHello->setFontSize(30);
		loginAppHello->setPosition(ccp(250, 350));
		//2:
		CCMenuItemFont* loginAppImportClientMessages = CCMenuItemFont::create("Loginapp_ImportClientMessages", this, menu_selector(AlphaScene::Loginapp_importClientMessages));
		loginAppImportClientMessages->setFontSize(30);
		loginAppImportClientMessages->setPosition(ccp(360, 250));
		//3:
		CCMenuItemFont* loginappreqCreateAccount = CCMenuItemFont::create("Loginapp_reqCreateAccount", this, menu_selector(AlphaScene::Loginapp_reqCreateAccount));
		loginappreqCreateAccount->setFontSize(30);
		loginappreqCreateAccount->setPosition(ccp(320, 150));

	//-----------------------------------------------------------------------------
	CCMenu* pMenu = CCMenu::create(pTitle, pConnect, pDisconnect, loginAppHello, loginAppImportClientMessages, loginappreqCreateAccount, NULL);
	if (pMenu)
	{
		pMenu->setPosition(ccp(0,0));
		addChild(pMenu);
	}

	//Login Frame 
	CCSprite* picon = CCSprite::create("Icon.png");
	if (picon)
	{
		picon->setPosition(ccp(150, 650));
		CCMoveBy* pMoveRight = CCMoveBy::create(5.5f, ccp(500, 0));
		CCMoveBy* pMoveLeft = CCMoveBy::create(5.5f, ccp(-500, 0));
		CCSequence* pSeq = CCSequence::create(pMoveRight, pMoveLeft, NULL);
		picon->runAction(CCRepeatForever::create(pSeq));
		addChild(picon);
	}
	return true;
}


Scene * AlphaScene::scene()
{
	Scene * scene = new AlphaScene();
	if (scene && scene->init())
	{
		scene->autorelease();
		return scene;
	}
	delete scene;
	return NULL;
}

void AlphaScene::onLoginappReqClose(CCObject* pSender)
{
	CCBuffer streamBuffer;
	KBESendMessage::Loginapp_reqClose(streamBuffer);
	KBEngineApp::shareKBEngineApp()->send(&streamBuffer);
}



void AlphaScene::onConnectClick(CCObject* pSender)
{
	KBEngineApp::shareKBEngineApp()->InitLoginAppAddres("192.168.144.87", 20013);
}

void AlphaScene::onDisconnectClick(CCObject* pSender)
{
	KBEngineApp::shareKBEngineApp()->disconnect();
}

void AlphaScene::Loginapp_hello(CCObject* pSender)
{
	CCBuffer streamBuffer;
}

void AlphaScene::Loginapp_importClientMessages(CCObject* pSender)
{
	CCBuffer streamBuffer;
}

void AlphaScene::Loginapp_reqCreateAccount(CCObject* pSender)
{
	CCBuffer streamBuffer;
	/*
	streamBuffer.writeUint16(3);		//MSG ID
	streamBuffer.writeUint16(msgLen); //数据长度

	streamBuffer.writeString("2");						//客户端类型
	streamBuffer.writeChar(0);

	streamBuffer.writeString(accountName.c_str());		//用户名
	streamBuffer.writeChar(0);
	streamBuffer.writeString(password.c_str());			//用户密码
	streamBuffer.writeChar(0);
	
	
	CCInetAddress oAddres;
	oAddres.setIp(ipAddres.c_str());
	oAddres.setPort(port);
	
	CCNetDelegate::setSoTimeout(fSoTimeout);
	CCNetDelegate::isConnected();
	CCNetDelegate::connect();
	CCNetDelegate::disconnect();
	CCNetDelegate::send(pBuffer);
	*/
}